/*模拟PHP的GET , 使用时, 可以直接 $_GET['get参数'], 就直接获得GET参数的值*/
var $_GET = (function () {
    var url = window.document.location.href.toString();
    var u = url.split("?");
    if (typeof (u[1]) == "string") {
        u = u[1].split("&");
        var get = {};
        for (var i in u) {
            var j = u[i].split("=");
            get[j[0]] = j[1];
        }
        return get;
    } else {
        return {};
    }
})();

/**
 * Created by wuye on 2017/12/11.
 */

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    }
};
var ztree;

var vm = new Vue({
    el: '#rrapp',
    data: {
        inputs: [{
                spokesman: null,
                checkbox: false
            },
            {
                spokesman: null,
                checkbox: false
            },
            {
                spokesman: null,
                checkbox: false

            },
            {
                spokesman: null,
                checkbox: false

            },
            {
                spokesman: null,
                checkbox: false

            },
            {
                spokesman: null,
                checkbox: false

            },
            {
                spokesman: null,
                checkbox: false

            },
            {
                spokesman: null,
                checkbox: false

            }, {
                spokesman: null,
                checkbox: false

            }, {
                spokesman: null,
                checkbox: false

            }, {
                spokesman: null,
                checkbox: false

            },
        ],

        // 缓存要修改的章节信息
        updateInterviewInfo: {

        },

        // 显示添加章节的dom节点
        showDom: 'list',
        interview: {
            headline: '加载中'
        },
        q: {
            headline: null
        },
        showList: true,
        title: null,
        // roleList:{},
        banner: {
            bannerpic: null
        },
        sels: [

        ]
    },
    created: function () {
        // 获取id
        var id = $_GET['id'];
        if (!id) {
            alert('系统错误,没有获取到访谈ID,请返回重试.');
            return false;
        }

        console.log(id, '访谈id');

        $.get(baseURL + "sys/interview/findById/" + id, function (r) {
            vm.interview = r.interview;
        });

        // 加载章节列表

        $(function () {
            $("#jqGrid").jqGrid({
                mtype: 'POST',
                url: baseURL + 'sys/interviewInfo/list',
                datatype: "json",
                colModel: [{
                        label: 'ID',
                        name: 'id',
                        index: "id",
                        width: 45,
                        key: true
                    },
                    {
                        label: '发言人',
                        name: 'spokesman',
                        width: 75
                    },
                    {
                        label: '发言内容',
                        name: 'spokesContent',
                        width: 200
                    },
                    {
                        label: '发言时间',
                        name: 'spokesDate',
                        width: 75
                    }
                ],
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList: [10, 30, 50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth: true,
                multiselect: true,
                pager: "#jqGridPager",
                jsonReader: {
                    root: "page.records",
                    page: "page.current",
                    total: "page.pages",
                    records: "page.total"
                },
                prmNames: {
                    page: "page",
                    rows: "limit",
                    order: "order",
                },
                postData: {
                    interviewId: id
                },
                gridComplete: function () {
                    //隐藏grid底部滚动条
                    $("#jqGrid").closest(".ui-jqgrid-bdiv").css({
                        "overflow-x": "hidden"
                    });
                }
            });
        });
    },
    methods: {
        submitForm: function (event) {
            event.preventDefault();
            // console.log(this);
            const _this = this;
            let arr=this.inputs
            console.log(arr)
            let newArr=[];
            for (var one in arr) {
                // console.log(_this.inputs[one].sectionname)
                if (arr[one].spokesman) {
                    newArr.push(arr[one]);
                    console.log(newArr)
                    console.log(newArr[one]);

                    var sfile = new FormData()
                    sfile.append('spokesman', newArr[one]['spokesman'])
                    sfile.append('interviewId', vm.interview.id);
                    // sfile.append('isPay', _this.inputs[one]['checkbox']);
                    sfile.append('spokesContent', newArr[one]['spokesContent']);
                    sfile.append('spokesDate', newArr[one].spokesDate);
                    sfile.append('id', '')


                    $.ajax({
                        url: baseURL + '/sys/interviewInfo/save',
                        // $refs: {'tdSection':vm.section,'filetext':this.section.filetext},
                        data: sfile,
                        // headers: {
                        //"Content-Type": "multipart/form-$refs"
                        // "Content-Type": "text/plain;charset=UTF-8"
                        // },
                        cache: false,
                        processData: false,
                        contentType: false,
                        type: 'post',
                        success: function (result) {
                            if(result.code === 0){
                                alert('添加成功!', function () {
                                    window.location.reload();
                                });
                            }else {
                                alert(result.msg);
                            }
                        },
                    })
                } else {
                    console.log('wuzhi')
                }

            }


            return false;
        },
        submitFormUpdateInfo: function (event) {
            event.preventDefault();
            // console.log(this);
            const _this = this;

            // console.log(_this.inputs[one].sectionname)


            var sfile = new FormData()
            sfile.append('spokesman', _this.updateInterviewInfo.spokesman)
            sfile.append('interviewId', vm.interview.id);
            sfile.append('spokesContent', _this.updateInterviewInfo.spokesContent);
            sfile.append('spokesDate', _this.updateInterviewInfo.spokesDate);
            sfile.append('id', _this.updateInterviewInfo.id)


            $.ajax({
                url: baseURL + '/sys/interviewInfo/save',
                data: sfile,
                cache: false,
                processData: false,
                contentType: false,
                type: 'post',
                success: function (result) {
                    if(result.code == 0){
                        alert('修改成功!', function () {
                            window.location.reload();
                        });
                    }else {
                        alert('修改失败!'+result.msg);                    
                    }
                },
                error: function (error) {
                    alert('修改失败!');
                }
            })



            return false;
        },
        addsection: function () {
            vm.showDom = 'addSec'

            var _thisID = $_GET['id'];
            vm.title = "添加访谈详情";
        },
        back() {
            window.history.go(-1);
        },
        updateSectionBtn: function () {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.title = "修改";

            // 加载单条章节详情
            $.get(baseURL + "sys/interviewInfo/findById/"+id, function(r){
                console.log(r,'r')
                vm.updateInterviewInfo = r.interviewInfo
                vm.showDom = 'update' 
            });
        },
        del: function () {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/interviewInfo/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function () {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'headline': vm.q.headline
                },
                page: page
            }).trigger("reloadGrid");
        }
    }
});